<template>
  <div class="model">
    <!-- 遮罩层 -->
    <div class="mask" @click="changeIsshowLoginModel(false)"></div>
    <!-- 登录框 -->
    <div class="login">
      <!-- 关闭按钮 -->
      <div class="close" @click="changeIsshowLoginModel(false)"></div>
      <!-- 导航 -->
      <div class="navs">
        <a
          href="#"
          :class="{ active: isshowLoginContent }"
          @click="isshowLoginContent = true"
        >
          手机号登录
        </a>
        |
        <a
          href="#"
          :class="{ active: !isshowLoginContent }"
          @click="isshowLoginContent = false"
        >
          微信登录
        </a>
      </div>
      <!-- 内容 -->
      <div class="content">
        <!-- 手机号登录 -->
        <div class="phoneLogin">
          <div class="row">
            <input
              type="text"
              placeholder="请输入手机号"
              class="inp"
              v-model="phoneNumber"
            />
          </div>
          <div class="row">
            <slide-verify
              :l="42"
              :r="20"
              :w="362"
              :h="140"
              :accuracy="10"
              :show="true"
              :imgs="picture"
              @success="onSuccess"
              @fail="onFail"
              @refresh="onRefresh"
              :style="{ width: '100%' }"
              class="slide-box"
              ref="slideBlock"
              :slider-text="msg"
            ></slide-verify>
          </div>
          <div class="row">
            <input type="text" placeholder="请输入短信验证码" class="inp" v-model="code1"/>
            <button class="btn btncode" @click="getCode" v-show="!isshowCount">获取验证码</button>
            <button class="btn btncode" v-show="isshowCount">倒计时{{count}}</button>
          </div>
          <div class="row">
            <button class="btn btnlogin" @click="loginFn">登录</button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { sendSMSAPI,PhoneReginAPI } from '@/request';
import { mapActions, mapMutations, mapState } from "vuex";
import { validatePhoneNumber } from "../utils/index";
export default {
  data() {
    return {
      //  默认显示手机登录
      isshowLoginContent: true,
      // 手机号
      phoneNumber: "",
      msg: "向右滑动",
      //   倒计时
      count: 60,
      //   显示倒计时
      isshowCount: false,
      // 验证码
      code1:''
    };
  },
  computed:{
      ...mapState({
          picture:state=>state.showModel.picture,
          isshowCart:state=>state.loginStatus.isshowCart
      })
  },
  methods: {
    ...mapMutations({
      changeIsshowLoginModel: "showModel/changeIsshowLoginModel",
      changeIsshowCart:'loginStatus/changeIsshowCart',
// changeIsshowToast:'showToast/changeIsshowToast'
    }),
    ...mapActions({
      asyncIsshowToast:'showToast/asyncIsshowToast'
    }),
   async getCode() {
    // this.toVarify();  //这样调用即使是错误了也还是发送了请求，我们要阻止这个请求:
    let flag=this.toVarify();  //此时传过来的flag是true
    if(!flag) return; //取反为false，然后结束

    // 3.发送请求  {phone；2334}
    let res=await sendSMSAPI({
        phone:this.phoneNumber
    })
    console.log("res",res);
if(!res) return;  //解决返回undefined报错
    // 但前面两部要是为真，那就发送验证码请求，为真的条件是code=0
    if(res.code==0){
      // 1.提示发送成功
      alert("发送成功");
//   4.文本倒计时
    this.isshowCount=true;
    let timer=setInterval(()=>{
        this.count--;
        if(this.count==0){
            clearInterval(timer);
            this.count=60;
            this.isshowCount=false
        }
    },1000)
    }
    
    },
// 封装手机号和滑块
toVarify(){
    // 1.验证手机号码
      if (!validatePhoneNumber(this.phoneNumber)) {
        // alert("请输入正确手机号码");

        this.asyncIsshowToast({
          isShow:true,
          title:"请输入正确手机号码",
          type:'danger'
        })
        

        return;
      }
      //  2.验证滑块验证码
      if(this.msg=='再试一次' || this.msg=="向右滑动"){
          // alert("请重新滑动滑动验证码");
           this.asyncIsshowToast({
          isShow:true,
          title:"请重新滑动滑动验证码",
          type:'danger'
        })
        
          return
      }
      return true  //函数不执行，默认返回undefined,让该函数返回一个true抛出去
},
    // 点击登录
 async  loginFn(){
    // 发现也要效验前面三个是否正确，而手机号码和滑动是一样的，使用封装这两部分
    //  if(!this.toVarify()) return;  //1.调用手机号和验证码封装函数
    //  2.判断验证码是否为空
    if(this.code1 == ''){
      alert("请输入手机验证码");
      return;
    }
    // 但三者都成功了的话：
    // 4.发送登录请求：
    let res=await PhoneReginAPI({
        verifyCode:this.code1,
            phone:this.phoneNumber,
    })
    if(!res) return
    console.log("登录成功",res);
    if(res.code==0){
      // 1.提示成功
      alert("登录成功");
      // 2.存槠token
      localStorage.setItem("x-auth-token",res['x-auth-token']);
      // 关闭模态框
      this.changeIsshowLoginModel(false);
      // 4.更新登录为购物车按钮
       this.changeIsshowCart(true);
    }
    },
    // 拼图成功
    onSuccess(times) {
      let ms = (times / 1000).toFixed(1);
      this.msg = "login success, 耗时 " + ms + "s";
    },
    // 拼图失败
    onFail() {
      this.onRefresh(); // 重新刷新拼图
    },
    // 拼图刷新
    onRefresh() {
      this.msg = "再试一次";
    },
  },
};
</script>
 
<style lang = "less" scoped>
.model {
  width: 100%;
  height: 100%;
  position: fixed;
  top: 0;
  left: 0;
  .mask {
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.3);
  }
  .login {
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
    right: 0;
    margin: auto;
    width: 555px;
    height: 423px;
    background: url("../assets/img/login-box-bg.png");
    .close {
      position: absolute;
      top: 10px;
      right: 65px;
      background: url("../assets/img/close.png");
      width: 22px;
      height: 22px;
      cursor: pointer;
    }
    .navs {
      margin-top: 30px;
      display: flex;
      justify-content: center;
      a {
        text-decoration: none;
        color: #bbb;
        padding: 0 10px;
      }
      .active {
        color: #2b2b2b;
      }
    }
    .content {
      width: 350px;
      margin: 20px auto;
      .phoneLogin {
        .row {
          display: flex;
          margin-bottom: 25px;
          .inp {
            flex: 1;
            height: 50px;
            text-indent: 1em;
            outline: none;
          }
          .btn {
            color: #2b2b2b;
            border: none;
            cursor: pointer;
          }
          .btncode {
            background-color: #ccc;
            margin-left: 10px;
          }
          .btnlogin {
            width: 100%;
            height: 50px;
            background-color: #0a328e;
          }
        }
      }
    }
  }
}

/* 滑块验证码的样式 */
/deep/.slide-box {
    width: 100%;
    position: relative;
    box-sizing: border-box;
    canvas {
        position: absolute;
        left: 0;
        top: -120px;
        display: none;
        width: 100%;
        box-sizing: border-box;
    }
    .slide-verify-block{
        width: 85px;
        height: 136px;
    }
    .slide-verify-refresh-icon {
        top: -120px;
        display: none;
    }
    &:hover {
        canvas {
            display: block;
        }
        .slide-verify-refresh-icon {
            display: block;
        }
    }
}
</style>